package com.itheima.serviceimpl;

import com.alibaba.dubbo.config.annotation.Service;
import com.itheima.dao.ReportDao;
import com.itheima.pojo.Port;
import com.itheima.service.ReportService;
import com.itheima.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import redis.clients.jedis.JedisPool;
import redis.clients.jedis.Tuple;

import java.util.*;

@Service(interfaceClass = ReportService.class)
@Transactional
public class ReportServiceimpl implements ReportService {

    @Autowired
    private ReportDao reportDao;
    @Autowired
    private JedisPool jedisPool;



    //根据传过来的月份查询每个月的会员数
    @Override
    public List<Integer> getmemberCount(List<String> list) {

        List<Integer> memberCount = new ArrayList<>();
        //遍利list获取到月份,
        for (String month : list) {
            String month_= month+"-31";
            Integer Count = reportDao.memberCount(month_);
            memberCount.add(Count);
        }
        return memberCount;
    }

    @Override
    public List<Map<String, Object>> getSetmealReport() {
        List<Map<String, Object>> setmealReport = reportDao.getSetmealReport();
        return setmealReport;
    }

//    运营数据统计
    @Override
    public Map getBusinessReportData() throws Exception {
        /*      reportDate:null,    当前日期
                todayNewMember :0,    今日新加会员
                totalMember :0,         会员总数
                thisWeekNewMember :0,   本周新加会员
                thisMonthNewMember :0,  本月新加会员
                todayOrderNumber :0,     今日预约总数
                todayVisitsNumber :0,    今日到诊数
                thisWeekOrderNumber :0,  本周预约总数
                thisWeekVisitsNumber :0,  本周到诊数
                thisMonthOrderNumber :0,  本月预约总数
                thisMonthVisitsNumber :0,  本月到诊数
                hotSetmeal :[]              热门套餐(取前四 - > 预约总数排名)    */

        Map<String,Object> map = new HashMap<>();

            //获取今日日期
            String Today = DateUtils.parseDate2String(DateUtils.getToday());
            map.put("reportDate",Today);
            //获取本周周一是第几日
            String ThisWeekMonday = DateUtils.parseDate2String(DateUtils.getThisWeekMonday());
            //获取本月第一天
            String FirstDay4ThisMonth = DateUtils.parseDate2String(DateUtils.getFirstDay4ThisMonth());
            //获取本周的最后一天是第几日
            String LastDayOfWeek = DateUtils.parseDate2String(DateUtils.getDelaythedate(DateUtils.getThisWeekMonday(), 6));

            //获取本月最后一天
            String MonthEnd = DateUtils.parseDate2String(DateUtils.getMonthEnd());

            // ---------------------------------------------------------------------------------------

//          todayNewMember :0,    今日新加会员
          Integer todayNewMember =   reportDao.gettodayNewMember(Today);
          map.put("todayNewMember",todayNewMember);
//            totalMember :0,         会员总数
            Integer totalMember = reportDao.totalMember();
            map.put("totalMember",totalMember);
//            thisWeekNewMember :0,   本周新加会员总数
          Integer thisWeekNewMember =   reportDao.getMemberCountByDate(ThisWeekMonday);
          map.put("thisWeekNewMember",thisWeekNewMember);
//            thisMonthNewMember :0,  本月新加会员
            Integer thisMonthNewMember = reportDao.getMemberCountByDate(FirstDay4ThisMonth);
            map.put("thisMonthNewMember",thisMonthNewMember);
//            todayOrderNumber :0,     今日预约总数
           Integer todayOrderNumber = reportDao.getTodayOrderNumber(Today);
           map.put("todayOrderNumber",todayOrderNumber);
//            todayVisitsNumber :0,    今日到诊数
          Integer  todayVisitsNumber = reportDao.gettodayVisitsNumber(Today);
          map.put("todayVisitsNumber",todayVisitsNumber);
//            thisWeekOrderNumber :0,  本周预约总数
            Map<String,String> WeekDateMap = new HashMap<>();
            WeekDateMap.put("ThisWeekMonday",ThisWeekMonday);
            WeekDateMap.put("LastDayOfWeek",LastDayOfWeek);
       // System.out.println(map.get(ThisWeekMonday));
        //System.out.println(map.get(LastDayOfWeek));
           Integer thisWeekOrderNumber = reportDao.getOrderCountByDate(WeekDateMap);
       //System.out.println(thisWeekOrderNumber);

           map.put("thisWeekOrderNumber",thisWeekOrderNumber);
//            thisWeekVisitsNumber :0,  本周到诊数
           Integer thisWeekVisitsNumber =  reportDao.VisitsCountByDate(WeekDateMap);
           map.put("thisWeekVisitsNumber",thisWeekVisitsNumber);
//            thisMonthOrderNumber :0,  本月预约总数
            Map<String,String> MonthDateMap = new HashMap<>();
            MonthDateMap.put("FirstDay4ThisMonth",FirstDay4ThisMonth);
            MonthDateMap.put("MonthEnd",MonthEnd);
            Integer thisMonthOrderNumber = reportDao.getOrderCountByDate(MonthDateMap);
            map.put("thisMonthOrderNumber",thisMonthOrderNumber);
//            thisMonthVisitsNumber :0,  本月到诊数
            Integer thisMonthVisitsNumber =  reportDao.VisitsCountByDate(MonthDateMap);
            map.put("thisMonthVisitsNumber",thisMonthVisitsNumber);

//            hotSetmeal :[]              热门套餐(取前四 - > 预约总数排名)
            List<Map<String,Object>> hotSetmeal = reportDao.gethotSetmeal();
            map.put("hotSetmeal",hotSetmeal);
            return map;



    }

    @Override
    public Map portCount(String format1, String format2) {
        Set<Tuple> port1 = jedisPool.getResource().zrangeWithScores("port", 0, -1);//接口名
        if(port1==null||port1.size()<=0){
            //System.out.println(format1);
            //System.out.println(format2);
            Map<String,String> map = new HashMap<>();
            map.put("start",format1);
            map.put("end",format2);
            List<Map<String,Object>> map1 = reportDao.portCount(map);//键:接口名  值:访问次数
            //存入数据库,并排行
            for (Map<String,Object> map2 : map1) {
                //System.out.println(map2.get("count"));
                Long _count = (Long) map2.get("count");
                double count = _count * 1.0;
                String port = (String) map2.get("port");
                jedisPool.getResource().zadd("port",count,port);
                /*jedisPool.getResource().zi*/
            }
            jedisPool.getResource().expire("port",10); //10秒清除一次数据,
            System.out.println("该数据来自数据库");
        }


        Set<Tuple> port = jedisPool.getResource().zrangeWithScores("port", 0, -1);//接口名
        ArrayList<String> portName = new ArrayList<>();
        ArrayList<Double> Count = new ArrayList<>();

        Map<String,Object> portCount = new HashMap<>();
        for (Tuple tuple : port) {
            double score = tuple.getScore();
            String element = tuple.getElement();
            portName.add(element);
            Count.add(score);
//            System.out.println(score);
//            System.out.println(element);
        }

        portCount.put("portName",portName);
        portCount.put("Count",Count);


        return portCount;
    }

    @Override
    public Map portCount1(String format1, String format2) {
       jedisPool.getResource().del("port1");
        //System.out.println(format1);
        //System.out.println(format2);
        Map<String,String> map = new HashMap<>();
        map.put("start",format1);
        map.put("end",format2);
        List<Map<String,Object>> map1 = reportDao.portCount(map);//键:接口名  值:访问次数
        //存入数据库,并排行
        for (Map<String,Object> map2 : map1) {
            //System.out.println(map2.get("count"));
            Long _count = (Long) map2.get("count");
            double count = _count * 1.0;
            String port = (String) map2.get("port");
            jedisPool.getResource().zadd("port1",count,port);
        }
        Set<Tuple> port = jedisPool.getResource().zrangeWithScores("port1", 0, -1);//接口名

        ArrayList<String> portName = new ArrayList<>();
        ArrayList<Double> Count = new ArrayList<>();

        Map<String,Object> portCount = new HashMap<>();
        for (Tuple tuple : port) {
            double score = tuple.getScore();
            String element = tuple.getElement();
            portName.add(element);
            Count.add(score);
//            System.out.println(score);
//            System.out.println(element);
        }

        portCount.put("portName",portName);
        portCount.put("Count",Count);


        return portCount;
    }
    @Override
    public List<Map<String, Object>> getMemberBySex() {
        List<Map<String, Object>> memberReport = reportDao.getMemberBySex();
        for (Map<String, Object> map : memberReport) {
            String name = (String) map.get("name");
            if (name.equals("1")){
                map.put("name","男");

            }else if (name.equals("2")){
                map.put("name","女");
            }
        }
        return memberReport;
    }

    @Override
    public List<Map<String, Object>> getMemberByAge() throws Exception {


        List<Map<String, Object>> memberReport=reportDao.getMemberByAge();
        return memberReport;
    }
}
